package com.tedu.openmedical.brregister.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tedu.openmedical.brregister.pojo.dto.AddRegister;
import com.tedu.openmedical.brregister.pojo.entity.BrRegister;
import com.tedu.openmedical.brregister.mapper.BrRegisterMapper;
import com.tedu.openmedical.brregister.pojo.vo.AddAuditorVo;
import com.tedu.openmedical.brregister.service.BrRegisterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tedu.openmedical.btAuditor.service.BtAuditorService;
import com.tedu.openmedical.utils.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 病案借阅登记表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-08-08
 */
@Service
public class BrRegisterServiceImpl extends ServiceImpl<BrRegisterMapper, BrRegister> implements BrRegisterService {

    private static final Logger log = LoggerFactory.getLogger(BrRegisterServiceImpl.class);
    @Autowired
    private BtAuditorService btAuditorService;
    //新增登记
    @Override
    public void addRegister(AddRegister addRegister) {
        BrRegister brRegister = new BrRegister();
        //BeanUtils：将前端传递的参数拷贝到实体类中
        BeanUtils.copyProperties(addRegister, brRegister);
        //setBrGlobalSerial：设置全局流水号
        brRegister.setBrGlobalSerial(Util.getFlowingNumber());
        //setBrEstimateBorrower，setBrEstimateBorrower：设置借阅日期和归还日期
        brRegister.setBrEstimateBorrower(Util.strToDate(addRegister.getBrEstimateBorrower(), "yyyy-MM-dd"));
        brRegister.setBrEstimateRestitution(Util.strToDate(addRegister.getBrEstimateRestitution(), "yyyy-MM-dd"));
        //setBrEnroll：设置登记时间
        brRegister.setBrEnroll(new Date());
        //setBrOperator：设置操作员 TODO:当前用户就是操作员
        brRegister.setBrOperator(brRegister.getBrBorrowerName());
        int resultInert = baseMapper.insert(brRegister);

        AddAuditorVo addAuditorVo = new AddAuditorVo();
        QueryWrapper<BrRegister> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gms_create");
        queryWrapper.last("limit 1");
        BrRegister Register = baseMapper.selectOne(queryWrapper);
        addAuditorVo.setBrId(Register.getBrId());
        addAuditorVo.setBrMedicalRecordStatus(Register.getBrMedicalRecordStatus());
        btAuditorService.addAuditor(addAuditorVo);

    }
    //更新病案状态
    @Override
    public void updateStatusPass(String btRegisterId) {
        //1.获取要更新的病案信息
        BrRegister brRegister = baseMapper.selectById(btRegisterId);
        //2.更新病案状态 1-已审核
        brRegister.setBrMedicalRecordStatus(1);
        baseMapper.updateById(brRegister);
    }
//    updateStatusReject：更新病案状态为驳回
    @Override
    public void updateStatusReject(String btRegisterId, String rejectReason) {
        //1.获取要更新的病案信息
        BrRegister brRegister = baseMapper.selectById(btRegisterId);
        //2.更新病案状态 1-已审核
        brRegister.setBrMedicalRecordStatus(2);
        brRegister.setBrRejectReason(rejectReason);
        baseMapper.updateById(brRegister);
    }
    //updateStatusCancel: 更新病案状态为取消
    @Override
    public void updateStatusCancel(String btRegisterId) {
        //1.获取要更新的病案信息
        BrRegister brRegister = baseMapper.selectById(btRegisterId);
        //2.更新病案状态 2-已取消
        brRegister.setBrMedicalRecordStatus(3);
        baseMapper.updateById(brRegister);
    }


}
